﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SVN.Net.Controllers
{
    public abstract class ControllerBase : Controller
    {
        
        #region " UID and PWD "

        protected string UID
        {
            get
            {
                return Session["uid"] == null ? string.Empty : Session["uid"].ToString();
            }
            set
            {
                Session["uid"] = value;
            }
        }

        protected string PWD
        {
            get
            {
                return Session["pwd"] == null ? string.Empty : Session["pwd"].ToString();
            }
            set
            {
                Session["pwd"] = value;
            }
        }

        #endregion

        public Models.ISVNProvider SVN
        {
            get;
            private set;
        }

        public bool IsFaking
        {
            get;
            private set;
        }

        public ControllerBase()
            : this(new Models.SVN())
        {
            IsFaking = false;
        }

        public ControllerBase(Models.ISVNProvider svn)
            : base()
        {
            SVN = svn;
            IsFaking = true;
        }

        #region " Error information in ViewData (Expired to extension method)"

        //private const string CST_ERRINFO_KEY = "ErrorInformation";

        //public bool HasError
        //{
        //    get
        //    {
        //        return (TempData[CST_ERRINFO_KEY] != null && TempData[CST_ERRINFO_KEY].GetType() == typeof(List<string>));
        //    }
        //}

        //public void SetErrorValue(string message)
        //{
        //    // Create the error message data into ViewData.
        //    if (!HasError)
        //        TempData[CST_ERRINFO_KEY] = new List<string>();
        //    List<string> el = TempData[CST_ERRINFO_KEY] as List<string>;
        //    // Add the message into this list.
        //    if (!el.Contains(message))
        //        el.Add(message);
        //}

        //public string[] GetErrorValue()
        //{
        //    if (HasError)
        //        return (TempData[CST_ERRINFO_KEY] as List<string>).ToArray();
        //    else
        //        throw new NotSupportedException("There is no error information in this controller.");
        //}

        //public void ClearErrorValue()
        //{
        //    if (TempData[CST_ERRINFO_KEY] != null)
        //        TempData.Remove(CST_ERRINFO_KEY);
        //}

        #endregion

        #region " Delete the working directory "

        protected void DeleteWorkingDir(string path)
        {
            if (!IsFaking)
            {
                // Set the attribute for all files here to normal recursivly.
                SetFileAttributeToNormal(path);
                // Delete all files and folders.
                System.IO.Directory.Delete(path, true);
            }
        }

        private void SetFileAttributeToNormal(string path)
        {
            foreach (var file in System.IO.Directory.GetFiles(path))
            {
                System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
            }
            foreach (var _path in System.IO.Directory.GetDirectories(path))
            {
                SetFileAttributeToNormal(_path);
            }
        }

        #endregion
    }
}
